package com.yx.system.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.plugins.Page;
import com.yx.common.utils.DateUtils;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.common.validator.Assert;
import com.yx.modules.api.entity.TbInterview;
import com.yx.modules.api.entity.TbInterviewInfo;
import com.yx.modules.api.service.TbInterviewInfoService;
import com.yx.modules.api.service.TbInterviewService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 访谈详情
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-06-14 14:59:11
 */
@RestController
@RequestMapping("sys/interviewInfo")
public class SysInterviewInfoController2 {
	@Autowired
	private TbInterviewInfoService interviewInfoService;

	@Autowired
	private TbInterviewService tbInterviewService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:interviewInfo:list")
	public R list(@RequestParam Map<String, Object> map){
		Query query = new Query(map);
		Page<TbInterviewInfo> pageUtil = new Page<TbInterviewInfo>(query.getPage(), query.getLimit());
		Page<TbInterviewInfo> page = interviewInfoService.queryPageList(pageUtil, query);
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/findById/{id}")
	@RequiresPermissions("sys:interviewInfo:findById")
	public R info(@PathVariable("id") Integer id){
		TbInterviewInfo interviewInfo = interviewInfoService.queryObject(id);
		
		return R.ok().put("interviewInfo", interviewInfo);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:interviewInfo:save")
	public R save(String id, Integer interviewId, String spokesman, String spokesContent, String spokesDate) throws ParseException {
		Assert.isBlank(spokesman,"发言人不能为空");
		Assert.isBlank(spokesContent,"发言内容不能为空");
		Assert.isNull(spokesDate,"发言日期不能为空");
		//查询访谈
		TbInterview tbInterview = tbInterviewService.queryObject(interviewId);
		if (tbInterview==null){
			return R.error("查无此访谈");
		}
		//初始化访谈记录
		TbInterviewInfo tbInterviewInfo = null;
		if (id.equals("")){
			tbInterviewInfo = new TbInterviewInfo();
			tbInterviewInfo.setInterviewId(tbInterview.getId());
			tbInterviewInfo.setSpokesContent(spokesContent);
			tbInterviewInfo.setSpokesman(spokesman);
			tbInterviewInfo.setSpokesDate(DateUtils.format(spokesDate));
			interviewInfoService.save(tbInterviewInfo);
			return R.ok();
		}else {
			tbInterviewInfo = interviewInfoService.queryObject(Integer.valueOf(id));
			if (tbInterviewInfo==null){
				return R.error("访谈详情查询失败");
			}
			tbInterviewInfo.setSpokesDate(DateUtils.format(spokesDate));
			tbInterviewInfo.setSpokesman(spokesman);
			tbInterviewInfo.setSpokesContent(spokesContent);
			interviewInfoService.update(tbInterviewInfo);
			return R.ok();
		}
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:interviewInfo:update")
	public R update(@RequestBody TbInterviewInfo interviewInfo){
		interviewInfoService.update(interviewInfo);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:interviewInfo:delete")
	public R delete(@RequestBody Integer[] ids){
		interviewInfoService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
